/*
 * 版权所有 (c) 华为技术有限公司 2022-2022
 */
package com.huawei.cameratest;

import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.util.Log;
import android.util.Size;
import android.view.SurfaceHolder;

import com.huawei.cameratest.Multi_instance.Utils;

import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;

import static com.huawei.cameratest.MultiImageReaderActivity.isRunning;
import static com.huawei.cameratest.util.MultiStreamTagUtil.toSize;

public class DecodeInputThread extends Thread{
    private final String TAG = this.getClass().getSimpleName();
    private long presentationTimeUs = 0;
    private byte[] decodeInput = null;
    private MyImageReader myImageReader;
    private String resolution;
    private SurfaceHolder surfaceHolder;

    public DecodeInputThread(SurfaceHolder surfaceHolder, MyImageReader myImageReader, String resolution){
        this.surfaceHolder = surfaceHolder;
        this.myImageReader = myImageReader;
        this.resolution = resolution;
    }

    @SuppressLint("WrongConstant")
    @Override
    public void run() {
        Log.i(TAG, "create mediaCodec " + resolution);
        Size size = toSize(resolution);
        MediaFormat mediaFormat = MediaFormat.createVideoFormat(Utils.MIME_TYPE_264, size.getWidth(), size.getHeight());
        // COLOR_FormatYUV420Flexible
        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 1228800); // 1228800 // getVideoBitRate(size.getWidth(), size.getHeight())
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
        mediaFormat.setString("vendor.hisi.fast-output-mode", "true");
        MediaCodec mediaCodecDecoder = null;
        try {
            mediaCodecDecoder = MediaCodec.createDecoderByType(Utils.MIME_TYPE_264);
            mediaCodecDecoder.configure(mediaFormat, surfaceHolder.getSurface(), null, 2);
            mediaCodecDecoder.start();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        while (isRunning) {
            try {
                if (mediaCodecDecoder != null) {
                    ArrayBlockingQueue<byte[]> h264BufferQueue = myImageReader.getH264BufferQueue();
                    decodeInput = h264BufferQueue.take();
                    if (null != decodeInput) {
                        ByteBuffer[] inputBuffers = mediaCodecDecoder.getInputBuffers();
                        Log.i(TAG, "decode resolution " + resolution);
                        Log.i(TAG, "decode buffer size" + inputBuffers.length);
                        // 释放掉超时的InputBuffer   TIMEOUT_USEC  -1
                        int inputBufferIndex = mediaCodecDecoder.dequeueInputBuffer(-1);
                        if (inputBufferIndex >= 0) {
                            ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
                            inputBuffer.clear();
                            inputBuffer.put(decodeInput);
                            presentationTimeUs = System.currentTimeMillis() * 1000;
                            mediaCodecDecoder.queueInputBuffer(inputBufferIndex, 0, decodeInput.length, presentationTimeUs, 0);
                        }
                        decodeInput = null;
                    }
                    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); // 30000
                    int outputBufferIndex = mediaCodecDecoder.dequeueOutputBuffer(bufferInfo, 0);
                    if (outputBufferIndex >= 0) {
                        mediaCodecDecoder.releaseOutputBuffer(outputBufferIndex, true);
                    }
                }
            } catch (InterruptedException e) {
                Log.i(TAG, "decode interrupted");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (mediaCodecDecoder != null) {
            Log.i(TAG, "mediacodedecoder release");
            mediaCodecDecoder.release();
            mediaCodecDecoder = null;
        }
    }
}
